package com.tbr.blockchain.model;

import com.tbr.blockchain.security.CryptoUtil;
import com.tbr.blockchain.security.RSACoder;

import java.security.NoSuchAlgorithmException;
import java.util.Map;

public class Wallet {
    private String privateKey;
    private String publicKey;


    public Wallet() {
    }
    public Wallet(String publicKey){
        this.publicKey=publicKey;
    }

    public Wallet(String privateKey, String publicKey) {
        this.privateKey = privateKey;
        this.publicKey = publicKey;
    }
    public static Wallet generateWallet(){
        Map<Integer,String> map;

        try {
            map=RSACoder.generateKeyPair();
            String publicKey=map.get(0);
            String privateKey=map.get(1);
            return new Wallet(privateKey,publicKey);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getAddress(){
        String publicKeyHash=hashPubKey(publicKey);
        return CryptoUtil.MD5(publicKeyHash);
    }
    public static String getAddress(String publicKey){
        String publicKeyHash=hashPubKey(publicKey);
        return CryptoUtil.MD5(publicKeyHash);
    }


    public  String getHashPubKey(){
        return CryptoUtil.SHA256(publicKey);
    }

    public static String hashPubKey(String publicKey){
        return CryptoUtil.SHA256(publicKey);
    }

    /**
     * 获取
     * @return privateKey
     */
    public String getPrivateKey() {
        return privateKey;
    }

    /**
     * 设置
     * @param privateKey
     */
    public void setPrivateKey(String privateKey) {
        this.privateKey = privateKey;
    }

    /**
     * 获取
     * @return publicKey
     */
    public String getPublicKey() {
        return publicKey;
    }

    /**
     * 设置
     * @param publicKey
     */
    public void setPublicKey(String publicKey) {
        this.publicKey = publicKey;
    }

    public String toString() {
        return "Wallet{privateKey = " + privateKey + ", publicKey = " + publicKey + "}";
    }
}